package com.gonsin.venus.meeting.logic.services;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.base.exceptions.MyException;
import com.gonsin.gview.annotations.actions.GAction;
import com.gonsin.gview.annotations.actions.GSession;
import com.gonsin.gview.annotations.data.GDataChanged;
import com.gonsin.gview.annotations.data.GDataSource;
import com.gonsin.venus.base.interfaces.enums.BaseResultCodes;
import com.gonsin.venus.base.logic.keyOnly.KeyOnlyUtils;
import com.gonsin.venus.base.logic.utils.PageableUtils;
import com.gonsin.venus.meeting.bean.*;
import com.gonsin.venus.meeting.interfaces.ITopicService;
import com.gonsin.venus.meeting.logic.mapper.*;
import com.gonsin.venus.meeting.po.TopicPo;
import com.gonsin.venus.meeting.po.TopicSearchPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TopicServiceImpl implements ITopicService {

    @Autowired private TopicMapper topicMapper;

    @GDataSource("searchTopic")
    @Override
    public GPageResult<TopicBean> searchTopic(TopicSearchPo searchPo, GPageRequest pageRequest) {
        if(StringUtils.isEmpty(pageRequest.getSort())){
            pageRequest.setSort("-create_time");
        }
        List<TopicBean> list = topicMapper.searchTopic(searchPo, PageableUtils.toPageableMyBatis(pageRequest));
        int count = topicMapper.countTopic(searchPo);
        return GPageResult.result(list, count, pageRequest);
    }

    /**
     * 批量查询议题信息
     *
     * @param topicKeys 议题主键KEY列表
     * @return 议题信息列表
     */
    @Override
    public List<TopicBean> batchGetTopic(List<String> topicKeys) {
        return topicMapper.selectBatchIds(topicKeys);
    }

    /**
     * 主键查询议题信息
     *
     * @param topicKey 议题主键KEY
     * @return 议题信息
     */
    @GDataSource("getTopic")
    @Override
    public TopicBean getTopic(String topicKey) {
        return topicMapper.getTopic(topicKey);
    }

    /**
     * 主键删除议题信息
     *
     * @param topicKey 议题主键KEY
     */
    @GAction("deleteTopicByKey")
    @GDataChanged("searchTopic")
    @Transactional
    @Override
    public void deleteTopic(String topicKey) {
        //删除议题
        topicMapper.deleteById(topicKey);
    }

    //TODO 议题批量删除
    @GAction("batchDeleteTopic")
    @GDataChanged("searchTopic")
    @Transactional
    @Override
    public int batchDeleteTopic(TopicSearchPo searchPo) {

        List<TopicBean> topics = topicMapper.searchTopic(searchPo, PageableUtils.toPageableMyBatis(GPageRequest.all()));
        List<String> topicKeys = topics.stream().map(TopicBean::getTopicKey).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(topicKeys)){
            topicMapper.deleteBatchIds(topicKeys);
        }
        return BaseResultCodes.OK.getValue();
    }

    /**
     * 添加议题信息
     * @param topicPo 议题表单对象
     * @return
     */
    @GAction("addTopic")
    @GDataChanged("searchTopic")
    @Override
    public TopicBean addTopic(@GSession(SessionKeys.USER_KEY) String userKey, TopicPo topicPo) {

        TopicBean topic = new TopicBean();
        BeanUtil.copyProperties(topicPo, topic, CopyOptions.create().setIgnoreNullValue(true));
        topic.setTopicKey(KeyOnlyUtils.general("topic"));
        topicMapper.insert(topic);
        
        return topic;
    }

    /**
     * 修改议题
     *
     * @param topicKey 议题主键KEY
     * @param topicPo  议题表单对象
     * @return
     */
    @GAction("updateTopic")
    @GDataChanged("searchTopic")
    @Override
    public TopicBean updateTopic(String topicKey, TopicPo topicPo) {
//        if (meetingCenterService.isTopicStarted(topicKey)) {
//            throw new MyException(MeetingResultCode.TOPIC_IS_RUNNING.getValue(), "议题正在运行");
//        }

        TopicBean topicBean = topicMapper.selectById(topicKey);
        if (topicBean == null) {
            throw new MyException(MeetingResultCode.TOPIC_NOT_FOUND.getValue(), "根据topicKey找不到数据");
        }

        BeanUtil.copyProperties(topicPo, topicBean, CopyOptions.create().setIgnoreCase(true).setIgnoreNullValue(true).setIgnoreError(true));
        topicMapper.updateById(topicBean);
        return topicBean;
    }

}
